package com.yzy.soft.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yzy.soft.common.persistence.dao.MetadataTableMapper;
import com.yzy.soft.common.persistence.model.MetadataTable;
import com.yzy.soft.system.controller.MetadataTableController;
import com.yzy.soft.system.dto.TableDataDto;
import com.yzy.soft.system.service.IMetadataTableService;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据中数据库信息服务实现类
 * </p>
 *
 * @author yinzhiyuan
 * @since 2018-01-28
 */
@Service
public class IMetadataTableServiceImpl   implements IMetadataTableService {
    private static final Logger logger= LoggerFactory.getLogger(IMetadataTableServiceImpl.class);

    @Autowired
    private MetadataTableMapper metadataTableMapper;

    @Override
//    @Cacheable(value="thisredis", key="'metadata::table_'+#id")
    public TableDataDto selectPage(Integer metadatabaseId, int page, int pageSize) {
        Wrapper<MetadataTable> metadataTableWrapper = new EntityWrapper<>();
        metadataTableWrapper.where("metadatabase_id={0}", metadatabaseId);
        page = (page - 1) * pageSize;
        pageSize = page + pageSize;
        List<MetadataTable> result = metadataTableMapper.selectPage(new RowBounds(page, pageSize), metadataTableWrapper);
        TableDataDto tableDataDto = new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(metadataTableMapper.selectCount(metadataTableWrapper));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    /**
     * ,condition="#id!=1"
     * @param id
     * @return
     */
    @Override
    @CacheEvict(value="thisredis", key="'metadata::table_'+#id")
    public Integer deleteById(Integer id) {
        Integer result=metadataTableMapper.deleteById(id);
        return result;
    }

    @Override
    @Cacheable(value="thisredis", key="'metadata::table_'+#id")
    public MetadataTable selectById(Integer id) {
        MetadataTable metadataTable=metadataTableMapper.selectById(id);
        return metadataTable;
    }

    @Override
    @CacheEvict(value="thisredis", key="'metadata::table_'+#id")
    public Integer updateById(MetadataTable metadataTable) {
        logger.info("IMetadataTableServiceImpl updateById:{}",metadataTable);
        metadataTable.setLastupdatetime(new Date());
        Integer result=metadataTableMapper.updateById(metadataTable);
        return result;
    }
}
